package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.ReviewRequestDto;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.purchase.*;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.PurchaseService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/purchase")
@AllArgsConstructor
public class PurchaseController {
    private final PurchaseService purchaseService;

    @PostMapping("/require/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainRequire(@PathVariable String id) {
        UploadInfo txInfo = purchaseService.uploadToChainRequire(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("/require")
    public DataResponse<String> createPurchaseRequire(@RequestHeader("Authorization") String authHeader,
                                                      @RequestBody PurchaseRequireDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        String res = purchaseService.createPurchaseRequire(dto);
        return DataResponse.success(res);
    }
    @PostMapping("/require/review/{id}")
    public DataResponse<String> reviewPurchaseRequire(@RequestHeader("Authorization") String authHeader,
                                                      @PathVariable String id, @RequestBody ReviewRequestDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String res = purchaseService.reviewPurchaseRequire(id, dto.getType(),userSigDto);
        return DataResponse.success(res);
    }

    @PutMapping("/require/{id}")
    public DataResponse<PurchaseRequireDto> updatePurchaseRequire(@RequestHeader("Authorization") String authHeader,
                                                                  @PathVariable String id,@RequestBody PurchaseRequireDto dto) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        PurchaseRequireDto res = purchaseService.updatePurchaseRequire(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/require/{id}")
    public DataResponse<String> deletePurchaseRequire(@PathVariable String id){
        String res = purchaseService.deletePurchaseRequire(id);
        return DataResponse.success(res);
    }
    @GetMapping("/require/list")
    public DataResponse<List<PurchaseRequireDto>> getListPurchaseRequire(@RequestHeader("Authorization") String authHeader){
        List<PurchaseRequireDto> res = purchaseService.getListPurchaseRequire();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = purchaseService.getListPurchaseRequireByEid(eid);
        }
        return DataResponse.success(res);
    }
    @GetMapping("/require/{id}")
    public DataResponse<PurchaseRequireDto> getByIdPurchaseRequire(@PathVariable String id) {
        PurchaseRequireDto res = purchaseService.getByIdPurchaseRequire(id);
        return DataResponse.success(res);
    }

    @PostMapping("/order/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainOrder(@PathVariable String id) {
        UploadInfo txInfo = purchaseService.uploadToChainOrder(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("/order")
    public DataResponse<String> createPurchaseOrder(@RequestHeader("Authorization") String authHeader,
                                                    @RequestBody PurchaseOrderDto dto){
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        String res = purchaseService.createPurchaseOrder(dto);
        return DataResponse.success(res);
    }

    @PutMapping("/order/{id}")
    public DataResponse<PurchaseOrderDto> updatePurchaseOrder(@RequestHeader("Authorization") String authHeader,
                                                              @PathVariable String id,@RequestBody PurchaseOrderDto dto) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        dto.setUserName(userSigDto.getUserName());
        dto.setUserId(userSigDto.getUserId());
        dto.setEnterpriseName(userSigDto.getEnterpriseName());
        dto.setEnterpriseId(userSigDto.getEnterpriseId());
        PurchaseOrderDto res = purchaseService.updatePurchaseOrder(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/order/{id}")
    public DataResponse<String> deletePurchaseOrder(@PathVariable String id){
        String res = purchaseService.deletePurchaseOrder(id);
        return DataResponse.success(res);
    }
    @GetMapping("/order/list")
    public DataResponse<List<PurchaseOrderDto>> getListPurchaseOrder(@RequestHeader("Authorization") String authHeader){
        List<PurchaseOrderDto> res = purchaseService.getListPurchaseOrder();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = purchaseService.getListPurchaseOrderByEid(eid);
        }
        return DataResponse.success(res);
    }
    @GetMapping("/order/{id}")
    public DataResponse<PurchaseOrderDto> getByIdPurchaseOrder(@PathVariable String id) {
        PurchaseOrderDto res = purchaseService.getByIdPurchaseOrder(id);
        return DataResponse.success(res);
    }

    @PostMapping("/material")
    public DataResponse<String> createPurchaseMaterial(@RequestBody PurchaseMaterialDto dto){
        String res = purchaseService.createPurchaseMaterial(dto);
        return DataResponse.success(res);
    }

    @PutMapping("/material/{id}")
    public DataResponse<PurchaseMaterialDto> updatePurchaseMaterial(@PathVariable String id,@RequestBody PurchaseMaterialDto dto) {
        PurchaseMaterialDto res = purchaseService.updatePurchaseMaterial(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/material/{id}")
    public DataResponse<String> deletePurchaseMaterial(@PathVariable String id){
        String res = purchaseService.deletePurchaseMaterial(id);
        return DataResponse.success(res);
    }
    @GetMapping("/material/list")
    public DataResponse<List<PurchaseMaterialDto>> getListPurchaseMaterial(){
        List<PurchaseMaterialDto> res = purchaseService.getListPurchaseMaterial();
        return DataResponse.success(res);
    }
    @GetMapping("/material/{id}")
    public DataResponse<PurchaseMaterialDto> getByIdPurchaseMaterial(@PathVariable String id) {
        PurchaseMaterialDto res = purchaseService.getByIdPurchaseMaterial(id);
        return DataResponse.success(res);
    }

    @PostMapping("/contract")
    public DataResponse<String> createPurchaseContract(@RequestBody PurchaseContractDto dto){
        String res = purchaseService.createPurchaseContract(dto);
        return DataResponse.success(res);
    }

    @PutMapping("/contract/{id}")
    public DataResponse<PurchaseContractDto> updatePurchaseMaterial(@PathVariable String id,@RequestBody PurchaseContractDto dto) {
        PurchaseContractDto res = purchaseService.updatePurchaseContract(id,dto);
        return DataResponse.success(res);
    }
    @DeleteMapping("/contract/{id}")
    public DataResponse<String> deletePurchaseContract(@PathVariable String id){
        String res = purchaseService.deletePurchaseContract(id);
        return DataResponse.success(res);
    }
    @GetMapping("/contract/list")
    public DataResponse<PurchaseContractListDto> getListPurchaseContract(){
        PurchaseContractListDto res = purchaseService.getListPurchaseContract();
        return DataResponse.success(res);
    }
    @GetMapping("/contract/{id}")
    public DataResponse<PurchaseContractDto> getByIdPurchaseContract(@PathVariable String id) {
        PurchaseContractDto res = purchaseService.getByIdPurchaseContract(id);
        return DataResponse.success(res);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> enterpriseErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }
}
